<template>
    <div class="order-list-container">
        <div class="controls-row">
            <h2>统计列表</h2>
        </div>

        <el-table :data="tableData" style="width: 100%" v-loading="loading" @row-click="handleRowClick">
            <el-table-column prop="id" label="ID" width="180" />
            <el-table-column prop="customerName" label="医生姓名" width="180" />
            <el-table-column prop="orderQuantity" label="订单数量">
                <template #default="scope">
                    {{ scope.row.orderQuantity.toLocaleString() }} 件
                </template>
            </el-table-column>
            <el-table-column prop="orderAmount" label="订单金额">
                <template #default="scope">
                    {{ scope.row.orderAmount.toLocaleString() }} 元
                </template>
            </el-table-column>
        </el-table>

        <div class="pagination-container">
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                :page-sizes="[10, 20, 50, 100]"
                layout="total, sizes, prev, pager, next"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElTable, ElTableColumn, ElPagination } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();

// 表格数据
const tableData = ref([]);
const loading = ref(false);

// 分页相关
const currentPage = ref(1);
const pageSize = ref(10);
const total = ref(0);

// 生成模拟数据
const generateMockData = (count) => {
    return Array.from({ length: count }, (_, index) => ({
        id: `ORD${String(index + 1).padStart(6, '0')}`,
        customerName: `医生${index + 1}`,
        orderQuantity: Math.floor(Math.random() * 50) + 1,
        orderAmount: Math.floor(Math.random() * 10000) + 1000
    }));
};

// 加载数据
const loadData = () => {
    loading.value = true;
    // 模拟API调用延迟
    setTimeout(() => {
        const mockData = generateMockData(100);
        total.value = mockData.length;
        
        // 计算分页数据
        const start = (currentPage.value - 1) * pageSize.value;
        const end = start + pageSize.value;
        tableData.value = mockData.slice(start, end);
        
        loading.value = false;
    }, 500);
};

// 处理分页大小变化
const handleSizeChange = (val) => {
    pageSize.value = val;
    loadData();
};

// 处理页码变化
const handleCurrentChange = (val) => {
    currentPage.value = val;
    loadData();
};

// 处理行点击事件
const handleRowClick = (row) => {
    // router.push(`/order-detail/${row.id}`);
    router.push(`/orderDetail`);
};

onMounted(() => {
    loadData();
});
</script>

<style scoped>
.order-list-container {
    padding: 20px;
}

.controls-row {
    margin-bottom: 20px;
}

.pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
}
</style>